package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminDao;
import ex.com.jm.food.entity.Admin;
/**
 * 管理员数据实现层
 * @author zhaojw
 *
 */
public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {

	/**
	 * 保存管理员
	 * @param admin
	 * @return
	 */
	@Override
	public Object saveAdmin(Admin admin) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(admin);
	}
	/**
	 * 根据管理员id查询某一个管理员
	 * @param id 管理员id
	 * @return
	 */
	@Override
	public Admin getAdmin(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Admin.class, id);
	}

	
	/**
	 * 管理员登录
	 * @param email 登录帐号
	 * @param password 登录密码
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Admin adminLogin(final String email,final String password) {
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				String hql="from Admin admin where admin.adminEmail=:email and admin.adminPwd=:password";
				Query query=session.createQuery(hql);
				query.setString("email", email);
				query.setString("password", password);
				return query.uniqueResult();
				// TODO Auto-generated method stub
			}
		});
		return (Admin) object;
	}
	/**
	 * 更新管理员
	 * @param admin
	 */


	/**
	 * 模糊查询 ，分页
	 * @param initPage 从第几条数据开始查
	 * @param pageRow  查询多少条数据，，即每页显示多少条数据
	 * @param name 根据姓名name实现模糊查询
	 * @param status 根据状态实现查询
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Admin> getAdmins(final int initPage,final int pageRow,final String name,
			final Integer status,final String order,final String sort) {
		System.err.println(pageRow);
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {
            
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.AdminDTO(admin.adminId,admin.adminName,admin.adminEmail,admin.adminPwd,"
						+ "case admin.type when 1 then '超级管理员' else '管理员' end,"
						+ "admin.status,admin.created,admin.updated,admin.isAdm) "
						+ " from Admin admin where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql +=" and admin.adminName like '%"+name+"%'"; 
				}
				if(status!=null&&!status.equals("")){
					hql+=" and admin.status ="+status;
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by "+order+" "+sort;
				}else{
					hql+=" order by created desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Admin>) object;
	}
	 /**
     * 注册或者添加时检验邮箱是否重复
     * @param email
     * @return
     */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Admin checkEmail(final String email) {
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
					String hql="from Admin admin where admin.adminEmail='"+email+"'";
				
				
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return (Admin) object;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Admin checkName(final String name) {
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Admin admin where admin.adminName='"+name+"' ";
	
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return (Admin) object;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int adminsCount(final String name,final Integer status) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Admin where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql +=" and adminName like '%"+name+"%'"; 
				}
				if(status!=null&&!status.equals("")){
					hql+=" and status ="+status;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}
	@Override
	public void updateAdmin(Admin admin) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().update(admin);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean updateAdminStatus(final Integer status,final String id) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Admin set status="+status+" where adminId='"+id+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	

}
